home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
SpriteEngine
/
SE Timer
/
SpriteHanders.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-12
|
4KB
|
150 lines
#include "SpriteTools.h"
// SpriteTools.h includes SpriteHandlers.h
/*** Custom handlers - application dependent ***
Edit this as necessary. It should always include the following three routines:
MoveSprite: move the sprite
HitSprite: handle collisions between two sprites
InitSprites: Load all faces and create initial sprites
***/
GrafPtr firstFace, secondFace, thirdFace;
void MoveSprite(SpritePtr theSprite)
{
long thisTime;
thisTime = TickCount();
while (theSprite->lastTime < thisTime)
{
theSprite->lastTime ++;
theSprite->speed.v++; // Simple gravity
theSprite->fixedPointPosition.h += theSprite->speed.h;
theSprite->fixedPointPosition.v += theSprite->speed.v;
theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
};
KeepOnScreenFixed(theSprite);
} /*MoveSprite*/
void HitSprite(SpritePtr theSprite, SpritePtr anotherSprite)
{
short tempSpeed;
if (RectSeparate(theSprite, anotherSprite) >= 2) // 2 or 3: horizontal, otherwise vertical
{
tempSpeed = theSprite->speed.h;
theSprite->speed.h = anotherSprite->speed.h;
anotherSprite->speed.h = tempSpeed;
theSprite->fixedPointPosition.h = theSprite->position.h << 4;
}
else
{
tempSpeed = theSprite->speed.v;
theSprite->speed.v = anotherSprite->speed.v;
anotherSprite->speed.v = tempSpeed;
theSprite->fixedPointPosition.v = theSprite->position.v << 4;
}
} /*HitSprite*/
void InitSprites()
{
SpritePtr theSprite;
/*Load all pictures*/
firstFace = LoadFaceFromCicn(128); /*cicn resource #128.*/
secondFace = LoadFaceFromCicn(129); /*cicn resource #129.*/
thirdFace = LoadFaceFromCicn(130); /*cicn resource #130.*/
/*Create sprites*/
theSprite = NewSprite();
theSprite->face = firstFace;
SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = secondFace;
SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = thirdFace;
SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
theSprite->lastTime = TickCount();
// extra
theSprite = NewSprite();
theSprite->face = firstFace;
SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = secondFace;
SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = thirdFace;
SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = firstFace;
SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = secondFace;
SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = thirdFace;
SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = firstFace;
SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = secondFace;
SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
theSprite = NewSprite();
theSprite->face = thirdFace;
SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
theSprite->lastTime = TickCount();
} /*InitSprites*/